using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Admin.NET.Test
{
    class TestDatabaseConnection
    {
        static void Main(string[] args)
        {
            Console.WriteLine("测试数据库连接...");
            
            // 测试主数据库连接
            TestConnection("主数据库", "Server=101.42.223.13;Database=test;Uid=root;Pwd=Gaoyuyang00`;SslMode=None;AllowLoadLocalInfile=true;AllowUserVariables=true;");
            
            // 测试日志数据库连接
            TestConnection("日志数据库", "Server=101.42.223.13;Database=testday;Uid=root;Pwd=Gaoyuyang00`;SslMode=None;AllowLoadLocalInfile=true;AllowUserVariables=true;");
            
            Console.WriteLine("测试完成，按任意键退出...");
            Console.ReadKey();
        }
        
        static void TestConnection(string dbName, string connectionString)
        {
            try
            {
                Console.WriteLine($"\n正在测试 {dbName} 连接...");
                Console.WriteLine($"连接字符串: {connectionString.Replace("Pwd=Gaoyuyang00`", "Pwd=***")}");
                
                using (var connection = new MySqlConnection(connectionString))
                {
                    connection.Open();
                    Console.WriteLine($"✅ {dbName} 连接成功！");
                    
                    // 测试查询
                    using (var command = new MySqlCommand("SELECT 1", connection))
                    {
                        var result = command.ExecuteScalar();
                        Console.WriteLine($"✅ {dbName} 查询测试成功，结果: {result}");
                    }
                    
                    // 检查表是否存在
                    using (var command = new MySqlCommand("SHOW TABLES LIKE 'knowledge_base'", connection))
                    {
                        var result = command.ExecuteScalar();
                        if (result != null)
                        {
                            Console.WriteLine($"✅ {dbName} knowledge_base 表存在");
                        }
                        else
                        {
                            Console.WriteLine($"⚠️ {dbName} knowledge_base 表不存在");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ {dbName} 连接失败: {ex.Message}");
                Console.WriteLine($"详细错误: {ex}");
            }
        }
    }
}
